package com.zxy.intellijpluin;

import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.CaretModel;

import javax.swing.*;
import java.util.Objects;

public class SearchWithBaidu extends AnAction {
    
    public SearchWithBaidu() {
        super(null, null, new ImageIcon(Objects.requireNonNull(SearchWithBaidu.class.getClassLoader().getResource("ico/baidu.png"))));
    }
    
    @Override
    public void actionPerformed(AnActionEvent e) {
        CaretModel caretModel = Objects.requireNonNull(e.getData(LangDataKeys.EDITOR)).getCaretModel();
        Caret currentCaret = caretModel.getCurrentCaret();
        String selectedText = currentCaret.getSelectedText();
        
        if (selectedText !=null) {
            BrowserUtil.browse("http://www.baidu.com/s?ie=utf-8&wd=" + selectedText );
            //try {
            //    Runtime.getRuntime().exec("C:\\Windows\\explorer.exe \"http://www.baidu.com/s?ie=utf-8&wd=" + selectedText + "\"");
            //} catch (IOException e1) {
            //    e1.printStackTrace();
            //}
        } else {
            //try {
            //    Runtime.getRuntime().exec("C:\\Windows\\explorer.exe \"http://www.baidu.com/s?ie=utf-8&wd=" + "" + "\"");
            //} catch (IOException e1) {
            //    e1.printStackTrace();
            //}
            BrowserUtil.browse("http://www.baidu.com/");
            
        }
    
    }
}
